package com.qjj.studydainty.fragment;

import android.content.ClipData;
import android.content.ClipboardManager;
import android.content.Context;
import android.os.Bundle;
import android.os.Message;
import android.view.ContextMenu;
import android.view.LayoutInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;

import androidx.fragment.app.Fragment;
import androidx.recyclerview.widget.DefaultItemAnimator;
import androidx.recyclerview.widget.DividerItemDecoration;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.qjj.studydainty.Db.DbUtil;
import com.qjj.studydainty.R;
import com.qjj.studydainty.adapter.MarkerAdapter;
import com.qjj.studydainty.entity.Marker;

import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

import static com.qjj.studydainty.entity.MyApplication.ACTIVITY_OBJECT;
import static com.qjj.studydainty.entity.MyApplication.OPEN_URL_IN_NEW_WINDOW;
import static com.qjj.studydainty.entity.MyApplication.handler;


public class MarkerFragment extends Fragment {

    private MarkerAdapter markerAdapter;
    private List<Marker> markerList = new ArrayList<>();

    @Override
    public boolean onContextItemSelected(MenuItem item) {
        int position = markerAdapter.getPosition();
        switch (item.getItemId()) {
            case R.id.open1:
                openUrl(position);
                break;
            case R.id.delete1:
                DbUtil.deleteByIdFromMarker(getActivity(), markerList.get(position).getId());
                markerAdapter.deleteData(position);
                break;
            case R.id.delete_all1:
                DbUtil.deleteAllFromMarker(getActivity());
                markerAdapter.deleteAllData();
                break;
            case R.id.copy1:
                copy(markerList.get(position).getUrl(), Objects.requireNonNull(getActivity()));
                break;
            default:
                return false;
        }
        return true;
    }

    private void openUrl(int position) {
        Message.obtain(handler, ACTIVITY_OBJECT, getActivity()).sendToTarget();
        Message.obtain(handler, OPEN_URL_IN_NEW_WINDOW, markerList.get(position).getUrl()).sendToTarget();
    }

    private void copy(String content, Context context) {
        ClipboardManager cmb = (ClipboardManager)
                context.getSystemService(Context.CLIPBOARD_SERVICE);
        if (null == cmb) {
            return;
        }
        cmb.setPrimaryClip(ClipData.newPlainText(null, content.trim()));
    }


    private void initDBData() {
        markerList = DbUtil.queryFromMarker(getContext());
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {

        View view = inflater.inflate(R.layout.fragment_marker, container, false);
        RecyclerView recyclerView = view.findViewById(R.id.history_recycleView);

        initDBData();

        markerAdapter = new MarkerAdapter(getContext(), markerList);

        recyclerView.setAdapter(markerAdapter);
        //设置布局管理
        recyclerView.setLayoutManager(new LinearLayoutManager(getContext()));
        //recyclerView.setLayoutManager(new LinearLayoutManager(getContext()));
        //设置动画
        recyclerView.setItemAnimator(new DefaultItemAnimator());
        //设置分割线
        recyclerView.addItemDecoration(new DividerItemDecoration(Objects.requireNonNull(getContext()), DividerItemDecoration.VERTICAL));//requireNonNull
        //recyclerView.addItemDecoration(new DividerItemDecoration(getContext(), DividerItemDecoration.HORIZONTAL));

        registerForContextMenu(recyclerView);

        markerAdapter.setOnItemClickListener(new MarkerAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(View view, int position) {
                openUrl(position);
            }

            @Override
            public void OnItemLongClick(View view, int position) {
            }
        });
        recyclerView.setOnCreateContextMenuListener(new View.OnCreateContextMenuListener() {
            @Override
            public void onCreateContextMenu(ContextMenu menu, View v, ContextMenu.ContextMenuInfo menuInfo) {
                menu.setHeaderTitle("书签");
                Objects.requireNonNull(getActivity()).getMenuInflater().inflate(R.menu.marker, menu);
            }
        });

        return view;
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        System.out.println("摧毁RecommendView");
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        System.out.println("摧毁Recommend");
    }

}
